Kernel.sandbox.define('main', {
	
	gpsStarted: false,
	gpsBusy: false,
	mRunningTitle: '',
	mRunningFSA: '',
		
	/* ################################
	## Audio Library
	#################################*/
			
	audioPlay: function() {
		var self = this;		
        var audio = $('#audio');
        
        $('#audio').attr("src", "modules/media_player/minecraft.mp3");

        audio[0].pause();
        audio[0].load(); 
        audio[0].play();
 	},
 	
 	audioStop: function() {
		var self = this;		
        var audio = $('#audio');

        audio[0].pause();
 	},
		
	/* ################################
	## GPS Library
	## NMEA 0183
	#################################*/

	//
	// rename to vehiclePosition...etc
	//
	
	gpsStartPoll: function() {
		var self = this;
		
		function getNMEAPacket() {
			self.gpsStarted = true;
			if (self.gpsBusy) {
				setTimeout(getNMEAPacket, 1000);
				return;
			}

			self.gpsBusy = true;
			$.post( "ajax/passthru.php", function( data ) {
				self.gpsBusy = false;
				var res = data.split(",");
				
				// time
				var hour = res[1].substr(0, 2);				
				var min = res[1].substr(2, 2);				
				var sec = res[1].substr(4, 2);
				var d = new Date();
				// convert UTC to local time
				// TODO: add gps time sync to system clock
				var gpstime = d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate()+" "+hour+":"+min+":"+sec+" UTC";
				var now = new Date(gpstime);
				self.broadcast('gps-time', now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
				
				// latitude
				var lat1 = res[3].substr(0, 2);
				var lat2 = res[3].substr(2, 8);
				lat2 = lat2.replace(".", "");
				lat2 = parseInt(lat2) / 60;
				lat2 = lat2.toString().replace(".", "");
				self.broadcast('gps-lat', lat1.concat("."+lat2.substr(0, 5).toString()+res[4]));
				
				//longitude
				var long1 = res[5].substr(0, 3);
				var long2 = res[5].substr(3, 7);
				long2 = long2.replace(".", "");
				long2 = parseInt(long2) / 60;
				long2 = long2.toString().replace(".", "");
				self.broadcast('gps-long', long1.concat("."+long2.substr(0, 5).toString()+res[6]));

				//speed
				self.broadcast('gps-speed', res[7]*.1852);
				if (res[8] !== "0.000") {
					self.broadcast('gps-course', res[8]);
				}
				if (self.gpsStarted) {
					setTimeout(getNMEAPacket, 900);
				}
			});				
		}
		
		if (!self.gpsStarted) {
			getNMEAPacket();
		}
	},
	
	gpsStopPoll: function() {
		var self = this;
		
		if (self.gpsStarted) {
			self.gpsStarted = false;
		}
	},
	
	gpsSatellitesLocked: function() {
		var self = this;
		
	}, 
	
	gpsLatitudeFault: function() {
		
	},
	
	gpsLongitudeFault: function() {
		
	},
	
	gpsAltitudeFault: function() {
		
	},
	
	/* ################################
	## Module Status Library
	#################################*/
				
	refreshStatusFeed: function() {
		var self = this;
		
		Kernel.doREST({
			type: 'GET',
			url: 'feed',
			success: function(data) {
				self.broadcast('status-feed-update', data);
			},
			failure: function(er) {
				
				// NOTE: No module in this example listens for error messages to errors will go unnoticed
				self.broadcast('error', "Couldn't fetch the feed: "+er);
			}
		});
	},
	
	updateStatus: function(status) {
		var self = this;
		
		Kernel.doREST({
			type: 'POST',
			url: 'status',
			params: { newstatus: status },
			success: function(data) {
				
				// Let everyone know that you updated your status
				self.broadcast('status-update', status);
				
				// Also update the feed
				self.refreshStatusFeed();
			},
			failure: function(er) {
				self.broadcast('error', "Couldn't update the status: "+er);
			}
		});
	}
});
    

